<template>
	<view class="box_ark_container">
		<Nav :bgColor="bgColor" title="盒柜" @back="back"></Nav>
		<magicCard headerTitle='当前余额' @headerTap="headerTap" class="card-wrapper">
			<template v-slot:headerRight>
				<view class="flex align-center">
					<text class="more-text">交易记录</text>
					<text class="iconfont1 icon-jiantou1 arrow-right-icon"></text>
				</view>
			</template>
			<template v-slot:body>
				<view class="card-body flex justify-between align-center">
					<text class="coin-icon"></text>
					<text class="account">{{accountInfo.coin}}</text>
					<navigator url="/magicpages/account_charge/account_charge" class="buy-btn">立即购买</navigator>
				</view>
			</template>
		</magicCard>
		<magicCard headerTitle='我的开盒' @headerTap="boxHeaderTap" class="card-wrapper">
			<template v-slot:body>
				<view class="flex record-wrapper">
					<navigator :url="'/magicpages/my_box/my_box?currentActive='+index" class="record-item flex justify-center align-center flex-column" v-for="(item,index) in boxInfo">
						<text class="value">{{item.amount}}</text>
						<text class="label">{{item.status}}</text>
					</navigator>
				</view>
			</template>
		</magicCard>
		<view class="magic-box-list">
			<template v-for="(item,index) in magicBoxInfo.list"  @handleClick="boxClick">
				<magicBag class="magic-box-item" @handleClick="handleClick(item)" :value="item" :key="index"></magicBag>
			</template>
		</view>
		<ns-loading :isRotate="true" v-if="magicBoxInfo.loading"></ns-loading>
		<view class="load-finish" v-if="magicBoxInfo.finish">亲，已经到底了哦~</view>
	</view>
</template>

<script>
	import Nav from "@/components/magic-box/magic-nav/index.vue"
	import navMixin from "@/common/js/nav.js"
	import magicCard from "@/components/magic-box/magic-card/index.vue"
	import magicBag from  "@/components/magic-box/magic-bag/index.vue"
	import pageApi from "@/common/js/pageApi.js"
	import account from "@/common/js/account.js"
	export default {
		mixins: [navMixin,pageApi,account],
		components: {
			Nav,
			magicCard,
			magicBag
		},
		data() {
			return {
				magicBoxInfo:{
					params:{
						page:0,
						page_num:10,
						display:[]
					},
					list:[],
					finish:false,
					loading:false,
				},
				boxInfo:[{},{},{},{}]
			};
		},
		onLoad() {
			this.getMagicBoxList()
		},
		onShow() {
			this.boxInfo=[{},{},{},{}]
			this.getBoxInfo()
		},
		onReachBottom() {
			this.getMagicBoxList()
		},
		methods: {
			handleClick(item){
				uni.navigateTo({
					url: `/pages/magic-box/detail/detail?magicBoxId=${item.id}`,
				})
			},
			getMagicBoxList(){
				this.magicBoxInfo.params.display=this.magicBoxInfo.list.map(item=>item.id)?this.magicBoxInfo.list.map(item=>item.id).join(","):""
				this.api(this.magicBoxInfo,"/api/fudai/boxlist")
			},
			async getBoxInfo(){
				try{
					const result=await this.$api.sendRequest({async:false,url:"/api/member/box"})
					result.data.forEach(item=>{
						switch(item.status){
							case "已回收":
							this.boxInfo.splice(3,1,item)
							break;
							case "待提货":
							this.boxInfo.splice(0,1,item)
							break;
							case "待收货":
							this.boxInfo.splice(1,1,item)
							break;
							case "已完成":
							this.boxInfo.splice(2,1,item)
							break;
						}
					})
					
				}catch(e){
					//TODO handle the exception
				}
			},
			headerTap() {
				uni.navigateTo({
					url:"/magicpages/coin_record/coin_record"
				})
			},
			boxHeaderTap() {
				uni.navigateTo({
					url:"/magicpages/my_box/my_box"
				})
			},
			boxClick(){
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #E9EBF9;
	}
	.box_ark_container {
		.more-text {
			margin-right: 10rpx;
			font-size: 24rpx;
			color: #666;
			font-weight: bold;
		}

		.arrow-right-icon {
			font-size: 22rpx;
			color: #666;
		}

		.card-wrapper {
			margin: 0 23rpx 24rpx 17rpx;
		}

		.card-body {
			padding: 0 23rpx 0 48rpx;
			height: 124rpx;

			.coin-icon {
				width: 60rpx;
				height: 60rpx;
				background: url("~@/static/image/coin-icon.png");
				background-size: 100% 100%;
			}

			.account {
				margin: 0 auto 0 9rpx;
				font-size: 32rpx;
				font-weight: bold;
				color: #444444;
			}

			.buy-btn {
				width: 138rpx;
				height: 47rpx;
				background: linear-gradient(90deg, #6185FF, #8B60FF);
				border-radius: 10rpx;
				font-size: 22rpx;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 47rpx;
				text-align: center;
			}
		}

		.record-wrapper {
			height: 165rpx;

			.record-item {
				flex: 1;
				height: 100%;

				.value {
					font-size: 32rpx;
					font-weight: bold;
					color: #444444;
					line-height: 29rpx;
				}

				.label {
					margin-top: 39rpx;
					font-size: 26rpx;
					font-weight: 400;
					color: #444444;
					line-height: 22rpx;
				}
			}
		}
		.magic-box-list{
			margin: 0 23rpx 0 17rpx;
			.magic-box-item{
				margin-bottom: 15rpx;
				&:last-child{
					margin-bottom: 0;
				}
			}
		}
	}
</style>
